/*
   Copyright 2009 Bégaudeau Stéphane

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

package faneclipse.wizards;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.dialogs.WizardNewFileCreationPage;

/**
 * @author BEGAUDEAU stephane
 */
public class FanFirstPageFileWizard extends WizardNewFileCreationPage {
	
	/**
	 * The constructor.
	 * @param selection The selection.
	 */
	public FanFirstPageFileWizard(final IStructuredSelection selection) {
		super("New Fan File", selection);
		setTitle("Create a new Fan file");
		setDescription("Enter the name of the file");
		setFileExtension("fan");
	}
	
	/**
	 * @return The input stream of the default ".fan" file.
	 * @see org.eclipse.ui.dialogs.WizardNewFileCreationPage#getInitialContents()
	 */
	@Override
	protected final InputStream getInitialContents() {
		String name = this.getFileName().trim().toLowerCase().substring(0, this.getFileName().lastIndexOf("."));
		name = name.substring(0, 1).toUpperCase() + name.substring(1, name.length());
		
		String date = this.now("dd MMMMM yyyy");
		String str = "\n//\n// History:\n//   "+ date + " Creation" +"\n//\n\n";
		str += "**************************************************************************\n";
		str += "** "+ name +"\n";
		str += "**************************************************************************\n";
		str += "class "+ name +"\n";
		str += "{\n\n}\n";
		ByteArrayInputStream stream = new ByteArrayInputStream(str.getBytes());
		return stream;
	}

	/**
	 * @param dateFormat the date format.
	 * @return The current date at the given format.
	 */
	public final String now(final String dateFormat) {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
		return sdf.format(cal.getTime());
	}
}
